import jade.content.lang.sl.SLCodec;
import jade.content.onto.basic.*;

import jade.core.*;
import jade.core.behaviours.*;

import jade.domain.*;
import jade.domain.introspection.*;
import jade.domain.FIPAAgentManagement.*;
import jade.domain.JADEAgentManagement.*;

import jade.lang.acl.*;

import jade.proto.*;

import jade.wrapper.*;

import java.util.*;

public class SleepAgent extends Agent
{
	
	jade.lang.acl.ACLMessage message;
	String receiveAgentName;
	String dataAgentName;
	//member variables go here
	Long timeStamp	=0L;
	Long time	=0L;
	boolean done	=false;
	String messageContent;
	String typeCheck;
	ArrayList <Container> group=new ArrayList <Container> (500);
	protected void setup()
	{
		System.out.println("Hello. My name is "+getLocalName());
		String location[] = getName().split("_");
		dataAgentName = "local_"+location[1];
		receiveAgentName ="Receive_"+location[1]+"_"+location[2]+"_"+location[3]; 
		addBehaviour(new containerCycleBehaviour());
		addBehaviour(new messageHandlerBehaviour());
	
	}

	protected void takeDown()
	{
		//Destrucion of agent goes here
	}
	
	private class containerCycleBehaviour extends CyclicBehaviour
	{
		public void action() 
		{
			//called each time through the behavior	
			//part one of the behavior
			//Runs through his list

			if(time<timeStamp)
			{
				for(int i=0;i<group.size();i++)
				{
					//run through your list.

					group.get(i).incrementClock();
					if((group.get(i).CLOCK_TIME%group.get(i).CYCLE_LENGTH)>group.get(i).SLEEP_TIME)
					{
						message = new jade.lang.acl.ACLMessage(jade.lang.acl.ACLMessage.INFORM);
						message.setLanguage("English");
						message.setOntology("Container-delivery-ontology");
						message.addReceiver(new AID(receiveAgentName, AID.ISLOCALNAME));
						message.setContent(group.get(i).toMessage("|"));
						send(message);
					}
				}
				time++;
			}
			else
			{
				if(!done)
				{
					message = new jade.lang.acl.ACLMessage(jade.lang.acl.ACLMessage.INFORM);
					message.setLanguage("English");
					message.setOntology("Container-continue-ontology");
					message.addReceiver(new AID(dataAgentName, AID.ISLOCALNAME));
					message.setContent("Done");
					send(message);
					done = true;
						
				}
			}
		}
	}

	private class messageHandlerBehaviour extends CyclicBehaviour
	{
		public void action() 
		{
			message = receive();
			if (message != null) 
			{
				if(message.getOntology().equals("Container-delivery-ontology"))
				{
					group.add(new Container(message.getContent(),"|","&"));
				}
				else
				{
					if(message.getOntology().equals("Container-continue-ontology"))
					{
						long temp = Long.parseLong(message.getContent());
						if(temp<timeStamp)
						{
							done = false;
							timeStamp = temp;
						}
					}
					else
					{
						if(message.getOntology().equals("Container-startup-ontology"))
						{
							//handle the startup message here
							messageContent = message.getContent();
							String token[] = messageContent.split(":");
							timeStamp = Long.parseLong(token[5]);
						}
					}
				}
				
				
			}
			else
			{
				block();
			}		
		}
	}
						
}